package com.zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * Date: 2024/6/4 23:06
 * Author: T_log
 * Description: 选举测试
 */
public class LeadLatchDemo {


    public static void main(String[] args) throws Exception {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", 5000, 3000, retryPolicy);


        client.start();
        System.out.println("已经成功的连接ZK服务了....");

        LeaderLatch leaderLatch = new LeaderLatch(client, "/MY/latch");
        leaderLatch.start();

        //直到他成为leader才会向下面继续走
        leaderLatch.await();

        boolean hasLeadership = leaderLatch.hasLeadership();
        System.out.println("是否成为leader了" + hasLeadership);

        Thread.sleep(Integer.MAX_VALUE);
    }
}
